/*
	1970-01-01 08:00:00 电子产品 北京时间时间开始 周四
	valueOf()  获取时间戳
*/
const zhWk = ['日', '一', '二', '三', '四', '五', '六']
const enWk = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
const jpWk = ['日', '月', '火', '水', '木', '金', '土']

// 值为0-6  周日-周六
export function getWeek(time) {
  let date = new Date(time)
  let today = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} 00:00:00`
  date = new Date(today)
  const days = Math.floor(date.valueOf()/1000 / 3600 / 24)
  return (days + 5) % 7
}

export function getTime(time, format) {
  const tm = (Number(time)/1000).toFixed(0)
  const obj = {
    DD: Math.floor(tm / 3600 / 24),
    hh: Math.floor(tm / 3600),
    mm: Math.floor((tm / 60) % 60),
    ss: tm % 60
  }
  const reg = /(DD)|(hh)|(mm)|(ss)/g
  const result = format.replace(reg, function (val) {
    const res = obj[val] <= 9 ? ('0' + obj[val]): obj[val]
    return res
  })
  return result
}

export function dateFormat (time, format) {
  const date = new Date(time)
  if (isNaN(date.valueOf())) return ''
  const wk = getWeek(date)
  const obj = {
    YYYY: date.getFullYear(),
    MM: date.getMonth() + 1, // month
    DD: date.getDate(), // day
    WK: wk,
    ZHWK: zhWk[wk],
    ENWK: enWk[wk],
    JPWK: jpWk[wk],
    hh: date.getHours(), // hour
    mm: date.getMinutes(), // minute
    ss: date.getSeconds(), // second
    qq: Math.floor((date.getMonth() + 3) / 3), // quarter
    SS: date.getMilliseconds(), // millisecond
  }
  const reg = /(YYYY)|(MM)|(DD)|(WK)|(ZHWK)|(ENWK)|(JPWK)|(hh)|(mm)|(ss)|(qq)|(SS)/g
  const result = format.replace(reg, function (val) {
    if(val === 'WK' || val === 'ZHWK' || val === 'ENWK' || val === 'JPWK') { return obj[val] }
    const res = obj[val] <= 9 ? ('0' + obj[val]) : obj[val]
    return res
  })
  return result
}
